package web;

import com.alibaba.fastjson.JSONArray;
import com.mysql.cj.util.StringUtils;
import pojo.SmbmsUser;
import service.SmbmsUserService;
import service.impl.SmbmsUserServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
@WebServlet(value = "/userCodeServlet")
public class UserCodeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws
            ServletException, IOException {
        this.doPost(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        req.setCharacterEncoding("utf-8");
//获取userCode的值
        String userCode=req.getParameter("userCode");
//调用业务层接口
        SmbmsUserService smbmsUserService=new SmbmsUserServiceImpl();
        Map<String,String> map=new HashMap<String,String>();
        if(StringUtils.isNullOrEmpty(userCode)){
            map.put("userCode","exit");
        }else{
            try {
                SmbmsUser smbmsUser =
                        smbmsUserService.queryUserByUserCode(userCode);
                if(null !=smbmsUser.getUserCode()){// 已经存在
                    map.put("userCode","exit");
                }else{ //不存在
                    map.put("userCode","noexit");
                }
                PrintWriter writer = resp.getWriter();
                writer.write(JSONArray.toJSONString(map));
                writer.flush(); //刷新
                writer.close(); //关闭流
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

